package com.cloudserver.springboot.controller;

import cn.hutool.system.UserInfo;
import com.cloudserver.springboot.Constants.CharacterInfo;
import com.cloudserver.springboot.Constants.PermissionInfo;
import com.cloudserver.springboot.Constants.UserSession;
import com.cloudserver.springboot.bean.Character;
import com.cloudserver.springboot.bean.Permission;
import com.cloudserver.springboot.service.PermissionService;
import com.cloudserver.springboot.service.impl.PermissionServiceImpl;
import com.cloudserver.springboot.utils.CommonResult;
import com.cloudserver.springboot.utils.MapUntil;
import com.cloudserver.springboot.vo.PermissionMenu;
import com.sun.istack.internal.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.data.relational.core.sql.In;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.security.auth.callback.CallbackHandler;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.Optional;

/**
 * @Description: PermissionController 类是处理菜单（权限）相关操作，包括查询菜单、修改菜单、删除等
 * @author zqq
 * @create 2020-10-08 9:18
 */
@RestController
@Slf4j
public class PermissionController {

    @Resource
    private PermissionService permissionService;

    /**
     * @Description: doMenu 处理查询动态菜单请求
     * @param: [account, session]
     * @return: com.cloudserver.springboot.controller.PermissionController
     * @auther: zqq
     * @date: 20/10/8 9:47
     */
    @GetMapping(value = "/menu/{username}")
    public CommonResult<PermissionMenu> doMenu(@PathVariable("username") @NotNull String account,
                                               HttpSession session){
        MapUntil userMap  = (MapUntil)session.getAttribute(UserSession.USER_MAP);
        Optional<MapUntil> opt = Optional.ofNullable(userMap);
        if (opt.isPresent()){
            Integer userId = (Integer)opt.get().get(account);
            Optional<Integer> optional = Optional.ofNullable(userId);
            /**
             * 如果userid为空，我们通过账户再查询一次id
             */
            if (!optional.isPresent()){
                userId = permissionService.getUserIdByAccount(account);
                if (userId != -1){
                    userMap.put(account, userId);
                    session.setAttribute(UserSession.USER_MAP, userMap);
                }
            }
            /**
             * 如果userid为-1，则需要重新登录
             */
            if (userId == -1){
                return new CommonResult(PermissionInfo.error_code, PermissionInfo.ERROR_MSG);
            }
            Character allById = permissionService.getAllById(userId);
            Optional<Character> byId = Optional.ofNullable(allById);
            if (byId.isPresent()){

            }else {

            }


        }else {

        }

        return null;
    }
}
